package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.service.IPostService;
import cn.leyaber.hrm.domain.Post;
import cn.leyaber.hrm.query.PostQuery;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/post")
//帖子
public class PostController {
    @Autowired
    public IPostService postService;
    /**
     * 批量删除
     */
    @PatchMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long>ids){
        try {
            postService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！"+e.getMessage());
        }
    }
    //修改状态
    @PostMapping("/Modify_status")
    public AjaxResult Modify_status(@RequestBody Post post){
        try {
            postService.Modify_status(post);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改成功！"+e.getMessage());
        }
    }
    /**
    * 保存和修改公用的
    * @param post  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Post post){
        try {
            if(post.getId()!=null){
                postService.updateById(post);
            }else{
                postService.insert(post);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            postService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Post get(@PathVariable("id")Long id)
    {
        return postService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Post> list(){

        return postService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Post> json(@RequestBody PostQuery query)
    {
        Page<Post> page = new Page<Post>(query.getPage(),query.getRows());
        page = postService.selectPage(page);
        return new PageList<Post>(page.getTotal(),page.getRecords());
    }
}
